package com.osig.ec.cfca;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cfca.paperless.client.bean.FieldBean;
import cfca.paperless.client.bean.ImageBean;
import cfca.paperless.client.bean.MultiDataBean;
import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessUniteClient;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

/**
 * @Description 联合接口(合成业务模板,合成业务数据,自动化签章)
 * @author clsu
 * @CodeReviewer TODO
 */
public class testPaperlessUnite5 {

	public static void main(String[] args) throws Exception {
		try {
			long timeStart = new Date().getTime();// 开始时间

			PaperlessUniteClient clientBean = new PaperlessUniteClient(PaperlessConfig.uniteUrl);
			// String keyStorePath = "./TestData/cert/localhost.jks";
			// clientBean.initSSLContext(keyStorePath, "11111111", keyStorePath,
			// "11111111");

			////////////////////////////////// 合成模板(synthesizeAutoTemplate)///////////////////////////////////
			String templateCode = "mb001";
			String fieldBeanListXml = PaperlessUtil.buildFieldBeanListXml(getFieldBeanList());
			String textBeanListXml = ""; // PaperlessUtil.buildTextBeanListXml(getTextBeanList());
			String imageBeanListXml = PaperlessUtil.buildImageBeanListXml(getImageBeanList());
			clientBean.addSynthesizeAutoTemplateParams(templateCode, fieldBeanListXml, textBeanListXml,
					imageBeanListXml);

			/////////////////////////// 如下为用户1手写签名相关的数据，如果没用用户1此处可以省略/////////////////////////////////////////////////
			byte[] handwritingImage1 = IoUtil.read(new FileInputStream("C:\\Users\\Pengfei\\Desktop\\新建文件夹\\牛朋飞.png"));
			byte[] handwritingData1 = handwritingImage1;
			// byte[] identificationFrontData = IoUtil.read(new
			// FileInputStream("./TestData/1.png"));
			// byte[] identificationBackData = IoUtil.read(new
			// FileInputStream("./TestData/2.png"));
			List<MultiDataBean> list1 = new ArrayList<MultiDataBean>();
			list1.add(new MultiDataBean(handwritingData1, "handwritingData.dat", "media-id", "0"));// 手写签名数据
			list1.add(new MultiDataBean(handwritingImage1, "handwritingImage.png", "2"));// 手写签名图片
			// list1.add(new MultiDataBean(identificationFrontData,
			// "identificationFrontImage.png", "2"));//身份证正面
			// list1.add(new MultiDataBean(identificationBackData,
			// "identificationBackImage.png", "2"));//身份证反面
			String proofHashAndMultiData1[] = PaperlessUtil.getProofHashAndMultiData(list1);

			SignLocation signLocation = new SignLocation(1, "客户签字", "R", 10, 0);// r
																				// <LocationStyle>
																				// 位置风格：上:U；下:D；左:L；右:R；中:C；默认：C，必填

			// z 证件类型
			SealUserInfo sealUserInfo = new SealUserInfo("张三", "Z", "00000000000000000", "15744659823", "北京", "签合同");
			String proofSealStrategyXml = StrategyUtil.createProofSealStrategyXml(handwritingImage1, sealUserInfo,
					signLocation, proofHashAndMultiData1[0]);
			// String sealLayer2Text = "宋江";
			// String sealLayer2TextStyle = "font-size:12;";
			// //文字章皮肤
			// proofSealStrategyXml =
			// StrategyUtil.createProofSealStrategyXml(sealLayer2Text,sealLayer2TextStyle,
			// sealUserInfo, signLocation, proofHashAndMultiData1[0]);
			// System.out.println(111);
			/////////////////////////// 以上为用户1的数据/////////////////////////////////////////////////

			///////////////////////////////// 蓋章(sealAutoPdf)////////////////////////////////////////

			// String sealCode = "ybccbtest";
			// String sealPassword = "11111111";
			// // 可以为空，不为空则此图片会替代此处印章的印模图片
			// SealUserInfo sealUserInfo = new SealUserInfo("宜宾市商业银行股份有限公司",
			// "宜宾市", "电子回单");
			// // 3=关键字签章
			// SignLocation signLocation = new SignLocation("宜宾市商业银行客户理财交易流水",
			// "L", 0, -30);
			// // 生成签章策略文件
			// String sealStrategyXml =
			// StrategyUtil.createSealStrategyXml(sealCode, sealPassword, "",
			// sealUserInfo, signLocation);
			///////////////////////////////////////////////////////////////////////////////////////////

			String compoundSealStrategyXml = "<List>" + proofSealStrategyXml + "</List>";
			String bizTypeCode = "";// 业务类型，比如：开户、开通网银
			String outputFilePath = "/CFCA/tomcat/outputfile/test/test201706161456.pdf";
			PdfBean pdfBean = new PdfBean("", "a", bizTypeCode, outputFilePath);// 系统自动存储,存储的路径自动生成
			String multiData = "";
			String pdfBeanXml = PaperlessUtil.buildPdfBeanXml(pdfBean);
			clientBean.addCompoundSealAutoPdfParams(pdfBeanXml, multiData, compoundSealStrategyXml);

			/////////////////////////////////////////////////////////////////////////////////////////

			// 操作员编码或者机构号
			String operatorCode = PaperlessConfig.operatorCode;
			// 渠道编码，可以为空
			String channelCode = PaperlessConfig.channelCode;

			byte[] resultData = clientBean.uniteReqAndRes(operatorCode, channelCode);

			String errorRsString = PaperlessUtil.isError(resultData);

			// 处理结果为正常，保存签章后的PDF文件到本地目录下
			if ("".equals(errorRsString)) {
				System.out.println("OK:sealAutoPdf");
				System.out.println(resultData.length);
				System.out.println(outputFilePath);

				// IoUtil.write("C:\\Users\\Pengfei\\Desktop\\test201706161100.pdf",
				// resultData);
			} else {
				// 处理结果为异常，打印出错误码和错误信息
				System.out.println("NG:sealAutoPdf");
				System.out.println(resultData.length);
				System.out.println(new String(resultData, "UTF-8"));
			}

			long timeEnd = new Date().getTime();// 结束时间
			System.out.println("time:" + (timeEnd - timeStart));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static List<FieldBean> getFieldBeanList() throws Exception {
		List<FieldBean> fieldBeanList = new ArrayList<FieldBean>();
		// fieldBeanList.add(new FieldBean("businessorg", "20170322011800001",
		// "", "text",1));
		// fieldBeanList.add(new FieldBean("businessdate", "2017年3月22日", "",
		// "text",1));
		// fieldBeanList.add(new FieldBean("businesstime", "12:12:12", "",
		// "text",1));
		// fieldBeanList.add(new FieldBean("businesssn", "20170322011800001",
		// "", "text",1));
		// fieldBeanList.add(new FieldBean("businesscode", "20170322011800001",
		// "", "text",1));
		// fieldBeanList.add(new FieldBean("businessname", "信贷业务测试", "",
		// "text",1));
		// fieldBeanList.add(new FieldBean("accountno", "62222222222222222222",
		// "", "text",1));
		// fieldBeanList.add(new FieldBean("accountname", "测试人员", "",
		// "text",1));
		// fieldBeanList.add(new FieldBean("customerno", "20170322011800001",
		// "", "text",1));
		// fieldBeanList.add(new FieldBean("accountName", "张凡", "", "text",1));
		// fieldBeanList.add(new FieldBean("idtype", "Z", "", "text",1));
		// fieldBeanList.add(new FieldBean("idno", "20170322011800001", "",
		// "text",1));
		// fieldBeanList.add(new FieldBean("mtcontext", "业务测试", "", "text",1));

		// fieldBeanList.add(new FieldBean("businessorg", "20170322011800001",
		// "", "text",2));
		// fieldBeanList.add(new FieldBean("businessdate", "2017年3月22日", "",
		// "text",2));
		// fieldBeanList.add(new FieldBean("businesstime", "12:12:12", "",
		// "text",2));
		//
		// fieldBeanList.add(new FieldBean("businessorg", "20170322011800001",
		// "", "text",3));
		// fieldBeanList.add(new FieldBean("businessdate", "2017年3月22日", "",
		// "text",3));
		// fieldBeanList.add(new FieldBean("businesstime", "12:12:12", "",
		// "text",3));

		// int i=1;
		// fieldBeanList.add(new FieldBean("acno", "20170322011800001", "",
		// "text",i));
		// fieldBeanList.add(new FieldBean("acname", "20170322011800001", "",
		// "text",i));
		// fieldBeanList.add(new FieldBean("prdate", "20170322011800001", "",
		// "text",i));
		// fieldBeanList.add(new FieldBean("prorg", "宜宾市商业银行成都支行", "",
		// "text",i));
		// fieldBeanList.add(new FieldBean("preq", "20170322011800001", "",
		// "text",i));
		// fieldBeanList.add(new FieldBean("1-1", "1", "", "text",i));
		// fieldBeanList.add(new FieldBean("1-2", "20170615", "", "text",i));
		// fieldBeanList.add(new FieldBean("1-3", "交易名称测试数据", "", "text",i));
		// fieldBeanList.add(new FieldBean("1-4", "交易渠道测试数据", "", "text",i));
		// fieldBeanList.add(new FieldBean("1-5", "9 999 999.99", "",
		// "text",i));
		// fieldBeanList.add(new FieldBean("1-6", "交易类型交易类型交易类型交易类型交易类型", "",
		// "text",i));
		// fieldBeanList.add(new FieldBean("1-7", "交易状态测试数据", "", "text",i));
		// fieldBeanList.add(new FieldBean("pageno", "1", "", "text",i));
		// fieldBeanList.add(new FieldBean("pageall", "1", "", "text",i));
		//
		//
		//
		//
		// fieldBeanList.add(new FieldBean("acno", "20170322011800002", "",
		// "text",i+1));
		// fieldBeanList.add(new FieldBean("acname", "20170322011800001", "",
		// "text",i+1));
		// fieldBeanList.add(new FieldBean("prdate", "20170322011800001", "",
		// "text",i+1));
		// fieldBeanList.add(new FieldBean("prorg", "宜宾市商业银行成都支行", "",
		// "text",i+1));
		// fieldBeanList.add(new FieldBean("preq", "20170322011800001", "",
		// "text",i+1));
		// fieldBeanList.add(new FieldBean("1-1", "2", "", "text",i+1));
		// fieldBeanList.add(new FieldBean("1-2", "20170616", "", "text",i+1));
		// fieldBeanList.add(new FieldBean("1-3", "交易名称测试数据", "", "text",i+1));
		// fieldBeanList.add(new FieldBean("1-4", "交易渠道测试数据", "", "text",i+1));
		// fieldBeanList.add(new FieldBean("1-5", "9 999 999.99", "",
		// "text",i+1));
		// fieldBeanList.add(new FieldBean("1-6", "交易类型交易类型交易类型交易类型交易类型", "",
		// "text",i+1));
		// fieldBeanList.add(new FieldBean("1-7", "交易状态测试数据", "", "text",i+1));
		// fieldBeanList.add(new FieldBean("pageno", "1", "", "text",i+1));
		// fieldBeanList.add(new FieldBean("pageall", "2", "", "text",i+1));
		//
		// fieldBeanList.add(new FieldBean("acno", "20170322011800003", "",
		// "text",i+2));
		// fieldBeanList.add(new FieldBean("acname", "20170322011800001", "",
		// "text",i+2));
		// fieldBeanList.add(new FieldBean("prdate", "20170322011800001", "",
		// "text",i+2));
		// fieldBeanList.add(new FieldBean("prorg", "宜宾市商业银行成都支行", "",
		// "text",i+2));
		// fieldBeanList.add(new FieldBean("preq", "20170322011800001", "",
		// "text",i+2));
		// fieldBeanList.add(new FieldBean("1-1", "3", "", "text",i+2));
		// fieldBeanList.add(new FieldBean("1-2", "20170617", "", "text",i+2));
		// fieldBeanList.add(new FieldBean("1-3", "交易名称测试数据", "", "text",i+2));
		// fieldBeanList.add(new FieldBean("1-4", "交易渠道测试数据", "", "text",i+2));
		// fieldBeanList.add(new FieldBean("1-5", "9 999 999.99", "",
		// "text",i+2));
		// fieldBeanList.add(new FieldBean("1-6", "交易类型交易类型交易类型交易类型交易类型", "",
		// "text",i+2));
		// fieldBeanList.add(new FieldBean("1-7", "交易状态测试数据", "", "text",i+2));
		// fieldBeanList.add(new FieldBean("pageno", "1", "", "text",i+2));
		// fieldBeanList.add(new FieldBean("pageall", "3", "", "text",i+2));

		// String address = "11的快捷付款捷付款的机发及啊啊啊啊啊啊啊啊啊啊";
		// fieldBeanList.add(new FieldBean("operator", "会计法", "", "text"));
		// fieldBeanList.add(new FieldBean("date", "2011年11月11", "", "text",
		// 2));
		// fieldBeanList.add(new FieldBean("branch", "ddddddddddddd", "",
		// "text", 2));
		// fieldBeanList.add(new FieldBean("fingerCode", "Sucaili", "", "text",
		// 2));
		// fieldBeanList.add(new FieldBean("signature1", "Sucaili", "",
		// "text",1));
		// fieldBeanList.add(new FieldBean("identNo", "Sucaili", "", "text",1));
		// fieldBeanList.add(new FieldBean("identType", "Sucaili", "", "text"));
		// fieldBeanList.add(new FieldBean("ICcardType", "Sucaili", "",
		// "text",1));
		// fieldBeanList.add(new FieldBean("accountName", "张凡的", "", "text",1));
		// fieldBeanList.add(new FieldBean("cardNo1", "Sucaili", "", "text",1));
		// fieldBeanList.add(new FieldBean("sequenceNo1", "Sucaili", "",
		// "text",1));
		// fieldBeanList.add(new FieldBean("address", address, "", "text",3));
		// fieldBeanList.add(new FieldBean("B1", "Sucaili", "", "text",1));

		fieldBeanList.add(new FieldBean("businesstime", "201706161102", "", "text"));
		fieldBeanList.add(new FieldBean("businessorg", "宜宾市商业银行", "", "text"));
		return fieldBeanList;
	}

	// private static List<TextBean> getTextBeanList() throws Exception {
	// List<TextBean> textBeanList = new ArrayList<TextBean>();
	// TextBean textBean1 = new TextBean("你好啊\r\n我的啊\r\ndddddd", "", "银行签章：",
	// "R", 0, 0);
	// textBean1.setTextFontSize("15");
	// textBean1.setTextColor("aa000000");
	// TextBean textBean2 = new
	// TextBean("545454545452212323\r\ndddddddddddddddddddddddd\r\n打开房间打开附件\r\ndkjfdkjf你的额尔",
	// "", "单据号：", "R", 0, 0);
	//// textBean2.setSpacingHeight(20);//设置行间距默认15像素
	// textBean2.setTextFontSize("15");
	// textBean2.setTextColor("aa000000");
	// textBeanList.add(textBean1);
	// textBeanList.add(textBean2);
	//
	// return textBeanList;
	// }
	//
	private static List<ImageBean> getImageBeanList() throws Exception {
		List<ImageBean> imageBeanList = new ArrayList<ImageBean>();
		// 如下代码是在pdf中添加可以显示的图片
		byte[] imageData0 = IoUtil.read(new FileInputStream("D:\\test\\1111.png"));
		ImageBean imageBean0 = new ImageBean("150", "150", "联网核查结果", "C", 80, 100, imageData0);
		imageBeanList.add(imageBean0);
		byte[] imageData1 = IoUtil.read(new FileInputStream("D:\\test\\1111.png"));
		ImageBean imageBean1 = new ImageBean("150", "150", "联网核查结果", "C", 350, 100, imageData1);
		imageBeanList.add(imageBean1);
		byte[] imageData2 = IoUtil.read(new FileInputStream("D:\\test\\1111.png"));
		ImageBean imageBean2 = new ImageBean("150", "150", "联网核查结果", "C", 80, -30, imageData2);
		imageBeanList.add(imageBean2);
		byte[] imageData3 = IoUtil.read(new FileInputStream("D:\\test\\1111.png"));
		ImageBean imageBean3 = new ImageBean("150", "150", "联网核查结果", "C", 350, -30, imageData3);
		imageBeanList.add(imageBean3);
		// String imagePath =
		// Base64.toBase64String("/temp/2.png".getBytes("utf-8"));
		// ImageBean imageBean = new ImageBean("100", "100", 1, "400", "300",
		// imagePath,2);
		// imageBean.setImagePath(imagePath);
		imageBeanList.add(imageBean0);
		// imageBeanList.add(imageBean);
		return imageBeanList;
	}

}
